home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / Tape Stuff / work.c < prev   
Encoding:
C/C++ Source or Header  |  1993-02-19  |  4.7 KB  |  226 lines  |  [TEXT/KAHL]

  1. /* this file interfaces between the apple interface code and the stuff that actually
  2.    does the hard work!
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include "SCSIPrototypes.h"
  10. #include "SCSIDefines.h"
  11.  
  12. #include "Defines.h"
  13. #include "Prototypes.h"
  14.  
  15. Str255    gParentDir;
  16. BUFFER    *gBufferPtr;
  17.  
  18. void Initialize(void)
  19. {
  20.     short     status;
  21.     short    id;
  22.     char     name[STRING];
  23.     
  24.     status = FindTape(name, &id);
  25.     if (status == OK) printf("Tape %s found at %hd\n", name, id);
  26.     else
  27.     {
  28.         printf("No tape devices found\n");
  29.         exit(0);
  30.     }
  31. }
  32.  
  33. void DoWork(short selection)
  34. {    
  35.     StandardFileReply     reply;
  36.     short                fileRefNum;
  37.     char                buffer[BLOCKSIZE];
  38.     long                count;
  39.     
  40.     switch (selection)
  41.     {
  42.     case WRITE_TAPE_ITEM:            /* write to tape */
  43.         
  44.         StandardGetFolder ("\pSelect folder to copy from:", &reply);
  45.         if (!reply.sfGood) break;
  46.         
  47.         Rewind();
  48.         gBufferPtr = BufferOpen();
  49.         
  50.         FullFileName(gParentDir, NULL, 
  51.             reply.sfFile.vRefNum, reply.sfFile.parID);
  52.  
  53.         SearchDirectory(reply.sfFile.parID, reply.sfFile.vRefNum, WriteFile, WriteFile);
  54.         WriteEOF();
  55.         BufferClose(gBufferPtr);
  56.  
  57.         break;
  58.  
  59.     case READ_TAPE_ITEM:            /* read from tape */
  60.         
  61.         StandardGetFolder ("\pSelect folder to copy to:", &reply);
  62.         if (!reply.sfGood) break;
  63.         
  64.         Rewind();
  65.         gBufferPtr = BufferOpen();
  66.         
  67.         FullFileName(gParentDir, NULL, 
  68.             reply.sfFile.vRefNum, reply.sfFile.parID);
  69.  
  70.         while (ReadFile() == OK);
  71.         BufferClose(gBufferPtr);
  72.  
  73.         break;
  74.  
  75.     case LIST_TAPE_ITEM:            /* list tape */
  76.         
  77.         Rewind();
  78.         gBufferPtr = BufferOpen();
  79.         while (ListFile() == OK);
  80.         BufferClose(gBufferPtr);
  81.         
  82.         break;
  83.  
  84.     case DUMP_TAPE_ITEM:            /* read tape to file */
  85.  
  86.         StandardPutFile("\pWrite disk file to tape:", "\pTape Dump", &reply);
  87.         
  88.         if (reply.sfGood)
  89.         {
  90.             FSpDelete(&reply.sfFile);
  91.             if (FSpCreate(&reply.sfFile, 'VIP@', 'TEXT', reply.sfScript) != noErr)
  92.             {
  93.                 printf("File create error\n");
  94.                 break;
  95.             }
  96.             if (FSpOpenDF(&reply.sfFile, fsCurPerm, &fileRefNum) != noErr)
  97.             {
  98.                 printf("File open error\n");
  99.             }
  100.             
  101.             Rewind();
  102.             
  103.             while (ReadBlock(buffer) == OK)
  104.             {
  105.                 count = BLOCKSIZE;
  106.                 if (FSWrite(fileRefNum, &count, buffer) != noErr) 
  107.                 {
  108.                     printf("File write error\n");
  109.                     break;
  110.                 }
  111.             }
  112.             FSClose(fileRefNum);
  113.         } 
  114.         break;
  115.             
  116.     case DUMP_FILE_ITEM:            /* write file to tape*/
  117.  
  118.         StandardGetFile(NULL, -1, NULL, &reply);
  119.         if (reply.sfGood)
  120.         {
  121.             if (FSpOpenDF(&reply.sfFile, fsCurPerm, &fileRefNum) != noErr)
  122.             {
  123.                 printf("File open error\n");
  124.             }
  125.             
  126.             Rewind();
  127.             
  128.             count = BLOCKSIZE;
  129.             while (FSRead(fileRefNum, &count, buffer) == noErr)
  130.             {
  131.                 if (WriteBlock(buffer) != OK) 
  132.                 {
  133.                     printf("Tape write error\n");
  134.                     break;
  135.                 }
  136.             }
  137.             if (count)    /* partial block */
  138.                 if (WriteBlock(buffer) != OK) printf("Tape write error\n");
  139.             FSClose(fileRefNum);
  140.             WriteMark(1);
  141.         } 
  142.         break;
  143.             
  144.     }
  145.     return;
  146. }
  147.  
  148.  
  149. short SearchDirectory(long parID, short vRefNum, 
  150.     short (*fileFunc)(Str255), short (*dirFunc)(Str255))
  151. {    
  152.     CSParam             pb;
  153.     FSSpec                 theResults[MAX_MATCHES];
  154.     char                 workBuf[MATCH_SPACE];
  155.     HFileInfo             spec1, spec2;
  156.     OSErr                 err;
  157.     Boolean             done;
  158.     short                 loopy;
  159.     HFileInfo             fInfo;
  160.     Str255                fullName;
  161.     DirInfo                cRec;
  162.  
  163.     pb.ioCompletion     = NULL;
  164.     pb.ioNamePtr         = NULL;
  165.     pb.ioVRefNum         = vRefNum;
  166.     pb.ioMatchPtr         = theResults;
  167.     pb.ioReqMatchCount     = MAX_MATCHES;
  168.     pb.ioSearchBits        = fsSBFlParID + fsSBFlAttrib;
  169.     pb.ioSearchInfo1    = (CInfoPBPtr)&spec1;
  170.     pb.ioSearchInfo2    = (CInfoPBPtr)&spec2;
  171.     pb.ioSearchTime        = -1;
  172.     pb.ioCatPosition.initialize    = 0;
  173.     pb.ioOptBuffer        = workBuf;
  174.     pb.ioOptBufSize        = MATCH_SPACE;
  175.         
  176.     memset(&spec1, 0, sizeof(spec1));
  177.     spec1.ioFlParID     = parID;
  178.     memset(&spec2, 0, sizeof(spec2));
  179.     spec2.ioFlParID     = parID;
  180.     spec2.ioFlAttrib    = IOFADIRECTORY; /* files */
  181.  
  182.     do
  183.     {
  184.         err = PBCatSearchSync(&pb);
  185.         done = (err == eofErr);
  186.         if (((err == noErr) || done) && (pb.ioActMatchCount > 0))
  187.         {
  188.             for (loopy = 0; loopy < pb.ioActMatchCount; loopy++)
  189.             {
  190.                 FullFileName(fullName, theResults[loopy].name, 
  191.                     theResults[loopy].vRefNum, theResults[loopy].parID);
  192.                 (*fileFunc)(fullName);
  193.             }
  194.         }
  195.     } while (!done);
  196.  
  197.     pb.ioCatPosition.initialize    = 0;
  198.     spec1.ioFlAttrib    = IOFADIRECTORY; /* directories */
  199.  
  200.     do
  201.     {
  202.         err = PBCatSearchSync(&pb);
  203.         done = (err == eofErr);
  204.         if (((err == noErr) || done) && (pb.ioActMatchCount > 0))
  205.         {
  206.             for (loopy = 0; loopy < pb.ioActMatchCount; loopy++)
  207.             {
  208.                 FullFileName(fullName, theResults[loopy].name, 
  209.                     theResults[loopy].vRefNum, theResults[loopy].parID);
  210.                 (*dirFunc)(fullName);
  211.                 
  212.                 memset(&cRec, 0, sizeof(cRec));
  213.                 cRec.ioNamePtr    = theResults[loopy].name;
  214.                 cRec.ioVRefNum    = theResults[loopy].vRefNum;
  215.                 cRec.ioDrDirID    = theResults[loopy].parID;
  216.                 PBGetCatInfo(&cRec, FALSE);
  217.                 SearchDirectory(cRec.ioDrDirID, cRec.ioVRefNum, fileFunc, dirFunc);
  218.             }
  219.         }
  220.     } while (!done);
  221.     return noErr;
  222. }
  223.  
  224.  
  225.  
  226.